<?php
class HTMLDocument{
	//ver=10170
	
	//Object __construct([url $tpl_name[,string $dt]])
	//void load(html $template)
	//void display()
	//void setTitle([string $title])
	//void addScript(url/code $js[, boolean $is_file])
	//Object getScript(url/code $js[,boolean $is_file])
	//void addStyle(url/string $code[, boolean $is_file])
	
	//Template constant.
	const TPL='<html><head><meta><link rel="icon" type="image/ico"><title></title></head><body></body></html>';
	
	//DOCTYPE Constants.
	const DT4='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
	const DT5='<!DOCTYPE html>';
	
	//The DOCTYPE of the HTML document.
	public $doctype;
	
	//The <html> element of HTML document.
	public $root;
	public $head;
	public $body;	

	//Language
	public $lang;
	//Character Set
	public $charset;
	//Page Icon
	public $icon;
	//Document Title
	public $title;
	
	//$template must be an html string.
	public function __construct($html_ver=5){
		//DOCTYPE
		if($html_ver==5)
			$this->doctype=self::DT5;
		if($html_ver==4)
			$this->doctype=self::DT4;
		//Root
		$root=new HTMLElement('html');
		$root->addAtt('lang','zh-cn');
		$this->root=$root;
		//Head
		$this->head=new HTMLElement('head');
		//Character Set
		$charset=new HTMLElement('meta');
		if($html_ver==5)
			$charset->addAtt('charset','utf-8');
		if($html_ver==4){
			$charset->addAtt('http-equiv','Content-Type');
			$charset->addAtt('content','text/html; charset=utf-8');
		}		
		$this->charset=$charset;
		//Icon
		$icon=new HTMLElement('link');
		$icon->addAtt('rel','icon');
		$icon->addAtt('type','image/ico');
		$this->icon=$icon;
		//Title
		$this->title=new HTMLElement('<title>Ceibit</title>');		
		//Body
		$this->body=new HTMLElement('body');
	}
	
	//Load a new template.
	public function reload($template,$is_file=TRUE){
		if($if_file){
			$tpl=trim(file_get_contents($template));
			$this->body->load($tpl);
		}
	}
	
	//Output the HTML code.
	public function display(){
		$this->head->addChild($this->charset);
		$this->head->addChild($this->icon);
		$this->head->addChild($this->title);
		$this->root->addChild($this->head);
		$this->root->addChild($this->body);
		echo $this->doctype,$this->root;
	}
	
	//Set the document's title.
	public function setTitle($title=''){
		$title=trim($title);
		if(!empty($title))
			$this->title=$title;
	}
	
	//Add javascript code to head.
	public function addScript($js,$is_file=TRUE){
		$js=trim($js);
		if(!empty($js)){
			$script=new HTMLElement('script');
			$script->addAtt("type","text/javascript");
			if($is_file)
				$script->addAtt("src","$js");
			else
				$script->addChild("$js");
			$this->head->addChild($script);
		}
	}
	//Return a <script> element.
	public function getScript($js,$is_file=TRUE){
		$js=trim($js);
		if(!empty($js)){
			$script=new HTMLElement('script');
			$script->addAtt("type","text/javascript");
			if($is_file)
				$script->addAtt("src","$js");
			else
				$script->addChild("$js");
			return $script;
		}
	}
	
	//Add CSS code to head.
	public function addStyle($code,$is_file=TRUE){
		$code=trim($code);
		if(!empty($code)){
			if($is_file){
				$link=new HTMLElement('link');
				$link->addAtt('rel','stylesheet');
				$link->addAtt('type','text/css');
				$link->addAtt('href',"$code");
				$this->head->addChild($link);
			}
			else{
				$style=new HTMLElement('style');
				$style->addAtt('type','text/css');
				$style->addChild($code);
				$this->head->addChild($style);
			}
		}
	}
}
?>